<template>
  <div class="hospital">
    <!-- 按钮 -->
    <div class="btns">
           <el-form :inline="true" size="small" >
        <el-form-item>
      <el-button type="primary" size="small" @click="toAdd">添加</el-button>
        </el-form-item>
        <el-form-item>
      <el-input v-model="param.name" size="small" placeholder="请输入医院名称"></el-input>
       </el-form-item>
       <el-form-item>
      <el-button type="primary" icon="el-icon-search" size="small" @click="loadHospitalData">搜索</el-button>
       </el-form-item>
      </el-form>
    </div>
    <!-- /按钮 -->

    <!-- 表格 -->
    <el-table size="small" :data="hospitals.list">
      <el-table-column label="序号" type="index" :index="1"></el-table-column>
      <el-table-column label="医院名称" prop="name"></el-table-column>
      <el-table-column label="医院简介" prop="introduce"></el-table-column>
      <el-table-column label="医院地址" prop="address"></el-table-column>
      <el-table-column label="标记" prop="flags"></el-table-column>
      <el-table-column label="经度" prop="longitude"></el-table-column>
      <el-table-column label="维度" prop="latitude"></el-table-column>
      <el-table-column label="操作" width="100" align="center" fixed="right">
        <template v-slot:default="scope">
          <el-button @click="toEdit(scope.row)" size="mini" type="text" icon="el-icon-edit" circle></el-button>
          <el-button @click="toDel(scope.row)" size="mini" type="text" icon="el-icon-delete" circle></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- /表格 -->
    <!-- 分页 -->
    <el-pagination
      v-if="hospitals.total > param.pageSize"
      layout="prev, pager, next"
      @current-change="currentChangeHandler"
      :current-page="param.page"
      :page-size="param.pageSize"
      :total="hospitals.total"
    ></el-pagination>
    <!-- /分页 -->

    <!-- 模态框 -->
    <el-dialog title="录入医院信息" :visible.sync="visible">
      <el-form label-width="80px" size="small">
        <el-form-item label="名称">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="介绍">
          <el-input type="textarea" v-model="form.introduce"></el-input>
        </el-form-item>
        <el-form-item label="省市">
          <el-cascader v-model="form.pca" :props="{label:'name',value:'name'}" :options="pca"></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址">
          <el-input v-model="form.address"></el-input>
        </el-form-item>
        <el-form-item label="标记">
          <el-input v-model="form.flags"></el-input>
        </el-form-item>
        <el-form-item label="经度">
          <el-input v-model="form.longitude"></el-input>
        </el-form-item>
        <el-form-item label="维度">
          <el-input v-model="form.latitude"></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="submit" size="small">确 定</el-button>
      </span>
    </el-dialog>
    <!-- /模态框 -->
  </div>
</template>

<script>
// 导入ajax工具（本质ajax -> axios -> reqeust）
import { get, post } from "@/utils/request";
import pca from "@/assets/pca.json";

export default {
  data() {
    return {
      pca,
      visible: false,
      param: {
        page: 1,
        pageSize: 10,
      },
      hospitals: {},
      form: {},
      title: "录入定点医院信息",
    };
  },
  created() {
    // 在vue实例创建完毕后加载医院信息
    this.loadHospitalData();
  },
  methods: {
    // 提交表单
    submit() {
      let url = "/hospital/saveOrUpdate";
      // 修改form，不用直接操作form，先进行克隆（深拷贝）
      let data = Object.assign({}, this.form);
      console.log(data);
      data.province = this.form.pca[0];
      data.city = this.form.pca[1];
      data.area=this.form.pca[2];
      delete data.pca;
      // 提交
      post(url, data).then((response) => {
        this.$message({ type: "success", message: response.message });
        this.loadHospitalData();
        this.visible = false;
      });
    },
    // 去添加
    toAdd() {
      this.title = "录入定点医院信息";
      this.form = {};
      this.visible = true;
    },
    // 打开模态框准备修改
    toEdit(row) {
      this.title = "修改定点医院信息";
      // 将row克隆后赋值给form，避免直接修改row影响页面变化
      this.form = Object.assign({}, row);
      this.form.pca = [row.province, row.city];
      this.visible = true;
    },
    toDel(row) {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        let url = "/hospital/deleteById";
        get(url, { id: row.id }).then((response) => {
          this.$message({ type: "success", message: response.message });
          this.loadHospitalData();
        });
      });
    },
    // 加载定点医院信息
    loadHospitalData() {
      let url = "/hospital/pageQuery";
      get(url, this.param).then((response) => {
        this.hospitals = response.data;
      });
    },
    // 监听翻页
    currentChangeHandler(page) {
      this.param.page = page;
      this.loadHospitalData();
    },
  },
};
</script>